/*
 * Copyright IBM Japan, AMS Services 2003 ALL Rights Reserved.
 *
 * This product includes software developed by
 * the Apache Software Foundation (http://www.apache.org/).
 */

package com.tianqu.tms.core.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.tianqu.tms.core.util.TmsConstants;

/**
 * 通过funcId取得画面的标题
 * @author liugang
 *
 */
public class PageTitleTag extends TagSupport {
	
	private static final long serialVersionUID = 5984632617532215381L;
	private String funcId;
	private Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 页面标题取得
	 */
	public int doStartTag() throws JspException {
		String result = null;
		JspWriter out = null;
		if (funcId == null) {
			throw new JspTagException("funcId is required");
		}
		// zhk liu.xingya start
		//获得menu对象
//		MenuBean MenuVO =
//			(MenuBean) pageContext.getServletContext().getAttribute(
//					TmsConstants.MENU_VO_KEY);
		MenuBean MenuVO =
			(MenuBean) pageContext.getRequest().getAttribute(
						TmsConstants.MENU_VO_KEY);
		if (MenuVO != null) {
			funcId = pageContext.getRequest().getParameter("funcId");
		} else {
			MenuVO = (MenuBean) pageContext.getServletContext().getAttribute(
					TmsConstants.MENU_VO_KEY);
		}
		// zhk liu.xingya end
		//menu对象是空
		if (MenuVO == null) {
			throw new JspTagException("Cannot find MenuBean");
		}
		
		MenuBean currentBean = MenuVO.getMenuVO(funcId);
		logger.debug(funcId);
		if(currentBean != null){
			result = currentBean.getMenuName();
			try {
				out = pageContext.getOut();
	
				if (result == null) {
					out.print("");
				} else {
					out.print(result);
				}
			} catch (IOException e) {
				throw new JspException("pagetitle : " + e.toString());
			}
		}
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * 
	 */
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}
	public String getFuncId() {
		return this.funcId;
	}
}
